home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 September / PCWorld_2002-09_cd.bin / Software / Vyzkuste / httrack / httrack-3.20RC4.exe / {app} / src / htsalias.c < prev    next >
C/C++ Source or Header  |  2002-07-09  |  16KB  |  495 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsalias.c subroutines:                                */
  34. /*       alias for command-line options and config files        */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "htsalias.h"
  42. #include "htsglobal.h"
  43. void linput(FILE* fp,char* s,int max);
  44. void hts_lowcase(char* s);
  45.  
  46. #define _NOT_NULL(a) ( (a!=NULL) ? (a) : "" )
  47. #define is_realspace(c) (strchr(" \x0d\x0a\x09",(c))!=NULL)
  48.  
  49. // COPY OF cmdl_ins in htsmain.c
  50. // Insert a command in the argc/argv
  51. #define cmdl_ins(token,argc,argv,buff,ptr) \
  52.   { \
  53.   int i; \
  54.   for(i=argc;i>0;i--)\
  55.   argv[i]=argv[i-1];\
  56.   } \
  57.   argv[0]=(buff+ptr); \
  58.   strcpy(argv[0],token); \
  59.   ptr += (strlen(argv[0])+1); \
  60.   argc++
  61. // END OF COPY OF cmdl_ins in htsmain.c
  62.  
  63.  
  64. /*
  65.   Aliases for command-line and config file definitions
  66.   These definitions can be used:
  67.   in command line:
  68.   --sockets=8       --cache=0
  69.   --sockets 8       --cache off
  70.                     --nocache
  71.   -c8               -C0
  72.   in config file:
  73.   sockets=8         cache=0
  74.   set sockets 8     cache off
  75.  
  76. */
  77. /*
  78.   single : no options
  79.   param  : this option allows a number parameter (1, for example) and can be mixed with other options (R1C1c8)
  80.   param1 : this option must be alone, and needs one distinct parameter (-P <path>)
  81.   param0 : this option must be alone, but the parameter should be put together (+*.gif)
  82. */
  83. const char hts_optalias[][4][64] = {
  84.   /*   {"","","",""}, */
  85.   {"path","-O","param1","output path"},
  86.   {"chroot","-%O","param1","default top path"},
  87.   {"mirror","-w","single",""},
  88.   {"mirror-wizard","-W","single",""},
  89.   {"get-files","-g","single",""},
  90.   {"quiet","-q","single",""},
  91.   {"mirrorlinks","-Y","single",""},
  92.   {"proxy","-P","param1","proxy name:port"},
  93.   {"httpproxy-ftp","-%f","param",""},
  94.   {"depth","-r","param",""},{"recurse-levels","-r","param",""},
  95.   {"ext-depth","-%e","param",""},
  96.   {"max-files","-m","param",""},
  97.   {"max-size","-M","param",""},
  98.   {"max-time","-E","param",""},
  99.   {"max-rate","-A","param",""},
  100.   {"max-pause","-G","param",""},
  101.   {"sockets","-c","param","number of simultaneous connections allowed"},{"socket","-c","param","number of simultaneous connections allowed"},{"connection","-c","param","number of simultaneous connections allowed"},
  102.   {"timeout","-T","",""},
  103.   {"retries","-R","param","number of retries for non-fatal errors"},
  104.   {"min-rate","-J","param",""},
  105.   {"host-control","-H","param",""},
  106.   {"extended-parsing","-%P","param",""},
  107.   {"near","-n","single",""},
  108.   {"test","-t","single",""},
  109.   {"list","-%L","param1",""},
  110.   {"language","-%l","param1",""}, {"lang","-%l","param1",""},
  111.   {"structure","-N","param",""}, {"user-structure","-N","param1",""},
  112.   {"long-names","-L","param",""},
  113.   {"keep-links","-K","param",""},
  114.   {"replace-external","-x","single",""},
  115.   {"no-passwords","-%x","single",""},{"no-password","-%x","single",""},
  116.   {"include-query-string","-%q","single",""},
  117.   {"generate-errors","-o","single",""},
  118.   {"purge-old","-X","param",""},
  119.   {"cookies","-b","param",""},
  120.   {"check-type","-u","param",""},
  121.   {"assume","-%A","param1",""}, {"mimetype","-%A","param1",""},
  122.   {"parse-java","-j","param",""},
  123.   {"robots","-s","param",""},
  124.   {"http-10","-%h","single",""},{"http-1.0","-%h","single",""},
  125.   {"no-compression","-%z","single",""},
  126.   {"tolerant","-%B","single",""},
  127.   {"updatehack","-%s","single",""}, {"sizehack","-%s","single",""},
  128.   {"user-agent","-F","param1","user-agent identity"},
  129.   {"footer","-%F","param1",""},
  130.   {"cache","-C","param","number of retries for non-fatal errors"},
  131.   {"store-all-in-cache","-k","single",""},
  132.   {"do-not-recatch","-%n","single",""},
  133.   {"do-not-log","-Q","single",""},
  134.   {"extra-log","-z","single",""},
  135.   {"debug-log","-Z","single",""},
  136.   {"verbose","-v","single",""},
  137.   {"file-log","-f","single",""},
  138.   {"single-log","-f2","single",""},
  139.   {"index","-I","single",""},
  140.   {"search-index","-%I","single",""},
  141.   {"priority","-p","param",""},
  142.   {"debug-headers","-%H","single",""},
  143.   {"userdef-cmd","-V","param1",""},
  144.   {"structure","-N","param1","user-defined structure"},
  145.   {"usercommand","-V","param1","user-defined command"},
  146.   {"display","-%v","single","show files transfered and other funny realtime information"},
  147.   {"dos83","-L0","single",""},
  148.   {"iso9660","-L2","single",""},
  149.   /* */
  150.  
  151.  
  152.   
  153.   /* STANDARD ALIASES */
  154.   {"spider","-p0C0I0t","single",""},
  155.   {"testsite","-p0C0I0t","single",""},
  156.   {"testlinks","-r1p0C0I0t","single",""}, {"test","-r1p0C0I0t","single",""}, {"bookmark","-r1p0C0I0t","single",""},
  157.   {"mirror","-w","single",""},
  158.   {"testscan","-p0C0I0Q","single",""}, {"scan","-p0C0I0Q","single",""}, {"check","-p0C0I0Q","single",""},
  159.   {"skeleton","-p1","single",""},
  160.   {"preserve","-%p","single",""},
  161.   {"get","-qg","single",""},
  162.   {"update","-iC2","single",""},
  163.   {"continue","-iC1","single",""}, {"restart","-iC1","single",""},
  164.   {"sucker","-r999","single",""},
  165.   {"help","-h","single",""}, {"documentation","-h","single",""}, {"doc","-h","single",""},
  166.   {"wide","-c32","single",""},
  167.   {"tiny","-c1","single",""},
  168.   {"ultrawide","-c48","single",""},
  169.   {"http10","-%h","single",""},
  170.   {"filelist","-%L","single",""}, {"list","-%L","single",""},
  171.   /* END OF ALIASES */
  172.  
  173.   /* Filters */
  174.   {"allow","+","param0","allow filter"},
  175.   {"deny","-","param0","deny filter"},
  176.   /* */
  177.  
  178.   /* URLs */
  179.   {"add","","param0","add URLs"},
  180.   /* */
  181.  
  182.   /* Specific */
  183.   {"user","-%U","param1","output path"},
  184.   /* */
  185.  
  186.   /* Internal */
  187.   {"catchurl","--catchurl","single","catch complex URL through proxy"},
  188.   {"updatehttrack","--updatehttrack","single","update HTTrack Website Copier"},
  189.   {"clean","--clean","single","clean up log files and cache"},
  190.   {"tide","--clean","single","clean up log files and cache"},
  191.   /* */
  192.  
  193.   {"","","",""}
  194. };
  195.  
  196.  
  197. /* 
  198.   Check for alias in command-line 
  199.   argc,argv     as in main()
  200.   n_arg         argument position
  201.   return_argv   a char[2][] where to put result
  202.   return_error  buffer in case of syntax error
  203.  
  204.   return value: number of arguments treated (0 if error)
  205. */
  206. int optalias_check(int argc,const char * const * argv,int n_arg,
  207.                    int* return_argc,char** return_argv,
  208.                    char* return_error) {
  209.   return_error[0]='\0';
  210.   *return_argc=1;
  211.   if (argv[n_arg][0]=='-')
  212.   if (argv[n_arg][1]=='-') {
  213.     char command[1000];
  214.     char param[1000];
  215.     char addcommand[256];
  216.     /* */
  217.     char* position;
  218.     int need_param=1;
  219.     //int return_param=0;
  220.     int pos;
  221.     command[0]=param[0]=addcommand[0]='\0';
  222.  
  223.     /* --sockets=8 */
  224.     if ( (position=strchr(argv[n_arg],'=')) ) {
  225.       /* Copy command */
  226.       strncat(command,argv[n_arg]+2,(int) (position - (argv[n_arg]+2)) );
  227.       /* Copy parameter */
  228.       strcpy(param,position+1);
  229.     }
  230.     /* --nocache */
  231.     else if (strncmp(argv[n_arg]+2,"no",2)==0) {
  232.       strcpy(command,argv[n_arg]+4);
  233.       strcpy(param,"0");
  234.     }
  235.     /* --sockets 8 */
  236.     else {
  237.       if (strncmp(argv[n_arg]+2,"wide-",5)==0) {
  238.         strcpy(addcommand,"c32");
  239.         strcpy(command,strchr(argv[n_arg]+2,'-')+1);
  240.       } else if (strncmp(argv[n_arg]+2,"tiny-",5)==0) {
  241.         strcpy(addcommand,"c1");
  242.         strcpy(command,strchr(argv[n_arg]+2,'-')+1);
  243.       } else
  244.         strcpy(command,argv[n_arg]+2);
  245.       need_param=2;
  246.     }
  247.  
  248.     /* Now solve the alias */
  249.     pos=optalias_find(command);
  250.     if (pos>=0) {
  251.       /* Copy real name */
  252.       strcpy(command,hts_optalias[pos][1]);
  253.       /* With parameters? */
  254.       if (strncmp(hts_optalias[pos][2],"param",5)==0) {
  255.         /* Copy parameters? */
  256.         if (need_param == 2) {
  257.           if ((n_arg+1>=argc) || (argv[n_arg+1][0]=='-')) {  /* no supplemental parameter */
  258.             sprintf(return_error,
  259.               "Syntax error:\n\tOption %s needs to be followed by a parameter: %s <param>\n\t%s\n",
  260.               command,command,_NOT_NULL(optalias_help(command)));
  261.             return 0;
  262.           }
  263.           strcpy(param,argv[n_arg+1]);
  264.           need_param=2;
  265.         }
  266.       } else
  267.         need_param=1;
  268.  
  269.       /* Final result */
  270.  
  271.       /* Must be alone (-P /tmp) */
  272.       if (strcmp(hts_optalias[pos][2],"param1")==0) {
  273.         strcpy(return_argv[0],command);
  274.         strcpy(return_argv[1],param);
  275.         *return_argc=2;     /* 2 parameters returned */
  276.       } 
  277.       /* Alone with parameter (+*.gif) */
  278.       else if (strcmp(hts_optalias[pos][2],"param0")==0) {
  279.         /* Command */
  280.         strcpy(return_argv[0],command);
  281.         strcat(return_argv[0],param);
  282.       }
  283.       /* Together (-c8) */
  284.       else {
  285.         /* Command */
  286.         strcpy(return_argv[0],command);
  287.         /* Parameters accepted */
  288.         if (strncmp(hts_optalias[pos][2],"param",5)==0) {
  289.           /* --cache=off or --index=on */
  290.           if (strcmp(param,"off")==0)
  291.             strcat(return_argv[0],"0");
  292.           else if (strcmp(param,"on")==0) {
  293.             // on is the default
  294.             // strcat(return_argv[0],"1");
  295.           } else
  296.             strcat(return_argv[0],param);
  297.         }
  298.         *return_argc=1;     /* 1 parameter returned */
  299.       }
  300.     } else {
  301.       sprintf(return_error,"Unknown option: %s\n",command);
  302.       return 0;
  303.     }
  304.     return need_param;
  305.   }
  306.  
  307.   /* Check -P <path> */
  308.   {
  309.     int pos;
  310.     if ((pos=optreal_find(argv[n_arg]))>=0) {
  311.       if ( (strcmp(hts_optalias[pos][2],"param1")==0) || (strcmp(hts_optalias[pos][2],"param0")==0)) {
  312.         if ((n_arg+1>=argc) || (argv[n_arg+1][0]=='-')) {  /* no supplemental parameter */
  313.           sprintf(return_error,
  314.             "Syntax error:\n\tOption %s needs to be followed by a parameter: %s <param>\n\t%s\n",
  315.             argv[n_arg],argv[n_arg],_NOT_NULL(optalias_help(argv[n_arg])));
  316.           return 0;
  317.         }
  318.         /* Copy parameters */
  319.         strcpy(return_argv[0],argv[n_arg]);
  320.         strcpy(return_argv[1],argv[n_arg+1]);
  321.         /* And return */
  322.         *return_argc=2;     /* 2 parameters returned */
  323.         return 2;           /* 2 parameters used */
  324.       }
  325.     }
  326.   }
  327.   
  328.   /* Copy and return other unknown option */
  329.   strcpy(return_argv[0],argv[n_arg]);
  330.   return 1;
  331. }
  332.  
  333. /* Finds the <token> option alias and returns the index, or -1 if failed */
  334. int optalias_find(const char* token) {
  335.   if (token[0] != '\0') {
  336.     int i=0;
  337.     while(hts_optalias[i][0][0] != '\0') {
  338.       if (strcmp(token,hts_optalias[i][0])==0) {
  339.         return i;
  340.       }
  341.       i++;
  342.     }
  343.   }
  344.   return -1;
  345. }
  346.  
  347. /* Finds the <token> real option and returns the index, or -1 if failed */
  348. int optreal_find(const char* token) {
  349.   if (token[0] != '\0') {
  350.     int i=0;
  351.     while(hts_optalias[i][0][0] != '\0') {
  352.       if (strcmp(token,hts_optalias[i][1])==0) {
  353.         return i;
  354.       }
  355.       i++;
  356.     }
  357.   }
  358.   return -1;
  359. }
  360.  
  361. const char* optreal_value(int p) {
  362.   return hts_optalias[p][1];
  363. }
  364. const char* optalias_value(int p) {
  365.   return hts_optalias[p][0];
  366. }
  367. const char* opttype_value(int p) {
  368.   return hts_optalias[p][2];
  369. }
  370. const char* opthelp_value(int p) {
  371.   return hts_optalias[p][3];
  372. }
  373.  
  374. /* Help for option <token>, empty if not available, or NULL if unknown <token> */
  375. const char* optalias_help(const char* token) {
  376.   int pos=optalias_find(token);
  377.   if (pos>=0)
  378.     return hts_optalias[pos][3];
  379.   else
  380.     return NULL;
  381. }
  382.  
  383. /* Include a file to the current command line */
  384. /* example:
  385.   set sockets 8
  386.   index on
  387.   allow *.gif
  388.   deny ad.*
  389. */
  390. int optinclude_file(const char* name,
  391.                     int* argc,char** argv,char* x_argvblk,int* x_ptr) {
  392.   FILE* fp;
  393.   fp=fopen(name,"rb");
  394.   if (fp) {
  395.     char line[256];
  396.     int insert_after=1;       /* first, insert after program filename */
  397.     while(!feof(fp)) {
  398.       char *a,*b;
  399.       int result;
  400.       
  401.       /* read line */
  402.       linput(fp,line,250);
  403.       hts_lowcase(line);
  404.       if (strnotempty(line)) {
  405.         /* no comment line: # // ; */
  406.         if (strchr("#/;",line[0])==NULL) {
  407.           /* right trim */
  408.           a=line+strlen(line)-1;
  409.           while(is_realspace(*a)) *(a--) = '\0';
  410.           /* jump "set " and spaces */
  411.           a=line;
  412.           while(is_realspace(*a)) a++;
  413.           if (strncmp(a,"set",3)==0) {
  414.             if (is_realspace(*(a+3))) {
  415.               a+=4;
  416.             }
  417.           }
  418.           while(is_realspace(*a)) a++;
  419.           /* delete = ("sockets=8") */
  420.           if ( (b=strchr(a,'=')) )
  421.             *b=' ';
  422.           
  423.           /* isolate option and parameter */
  424.           b=a;
  425.           while( (!is_realspace(*b)) && (*b) ) b++;
  426.           if (*b) {
  427.             *b='\0';
  428.             b++;
  429.           }
  430.           /* a is now the option, b the parameter */
  431.           
  432.           {
  433.             int return_argc;
  434.             char return_error[256];
  435.             char  _tmp_argv[4][HTS_CDLMAXSIZE];
  436.             char*  tmp_argv[4];
  437.             tmp_argv[0]=_tmp_argv[0]; tmp_argv[1]=_tmp_argv[1]; tmp_argv[2]=_tmp_argv[2]; tmp_argv[3]=_tmp_argv[3];
  438.             strcpy(tmp_argv[0],"--");
  439.             strcat(tmp_argv[0],a);
  440.             strcpy(tmp_argv[1],b);
  441.             
  442.             result=optalias_check(2,(const char * const *)tmp_argv,0,
  443.               &return_argc,(tmp_argv+2),
  444.               return_error);
  445.             if (!result) {
  446.               printf("%s\n",return_error);
  447.             } else {
  448.               int insert_after_argc;
  449.               /* Insert parameters BUT so that they can be in the same order */
  450.               /* temporary argc: Number of parameters after minus insert_after_argc */
  451.               insert_after_argc=(*argc)-insert_after;
  452.               cmdl_ins((tmp_argv[2]),insert_after_argc,(argv+insert_after),x_argvblk,(*x_ptr));
  453.               *argc=insert_after_argc+insert_after;
  454.               insert_after++;
  455.               /* Second one */
  456.               if (return_argc>1) {
  457.                 insert_after_argc=(*argc)-insert_after;
  458.                 cmdl_ins((tmp_argv[3]),insert_after_argc,(argv+insert_after),x_argvblk,(*x_ptr));
  459.                 *argc=insert_after_argc+insert_after;
  460.                 insert_after++;
  461.               }
  462.               /* increment to nbr of used parameters */
  463.               /* insert_after+=result; */
  464.             }
  465.           }
  466.         }
  467.         
  468.       }
  469.     }
  470.     fclose(fp);
  471.     return 1;
  472.   }
  473.   return 0;
  474. }
  475.  
  476. /* Get home directory, '.' if failed */
  477. /* example: /home/smith */
  478. char* hts_gethome(void) {
  479.   char* home = getenv( "HOME" );
  480.   if (home)
  481.     return home;
  482.   else
  483.     return ".";
  484. }
  485.  
  486. /* Convert ~/foo into /home/smith/foo */
  487. void expand_home(char* str) {
  488.   if (str[0] == '~') {
  489.     char tempo[HTS_URLMAXSIZE*2];
  490.     strcpy(tempo,hts_gethome());
  491.     strcat(tempo,str+1);
  492.     strcpy(str,tempo);
  493.   }
  494. }
  495.